from PIL import Image
import os
import sys

# 设置控制台输出编码为utf-8
sys.stdout.reconfigure(encoding='utf-8')

def remove_watermark(input_image_path, output_image_path, watermark_height=50):
    """
    去除图片右下角的水印
    
    参数:
        input_image_path: 输入图片路径
        output_image_path: 输出图片路径
        watermark_height: 水印的高度（默认50像素）
    """
    try:
        # 打开原始图片
        with Image.open(input_image_path) as img:
            # 获取图片尺寸
            width, height = img.size
            
            # 创建一个新的图片，去掉底部水印部分
            new_height = height - watermark_height
            new_img = img.crop((0, 0, width, new_height))
            
            # 保存处理后的图片
            new_img.save(output_image_path, quality=95)
            print(f'已成功去除水印并保存为: {output_image_path}')
            
    except Exception as e:
        print(f'处理图片时发生错误: {str(e)}')

if __name__ == '__main__':
    # 设置输入和输出文件路径
    input_image = '3.泥融飞燕子.png'
    output_image = '3.泥融飞燕子_no_watermark.png'
    
    # 检查输入文件是否存在
    if not os.path.exists(input_image):
        print(f'错误：找不到输入文件 {input_image}')
    else:
        # 去除水印
        remove_watermark(input_image, output_image)
